/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;

using Microsoft.Xna.Framework;

namespace Cobalt
{

	internal static class Helpers
	{

		public static float SmoothStep( float a, float b, float t )
		{
			int sign = Math.Sign( t );
			return MathHelper.SmoothStep( a, b, t * sign ) * sign;
		}

		public static Vector2 SmoothStep( Vector2 a, Vector2 b, Vector2 t )
		{
			return new Vector2( SmoothStep( a.X, b.X, t.X ), SmoothStep( a.Y, b.Y, t.Y ) );
		}

		public static void DisposeAndNull<T>( ref T obj )
		{
			try
			{
				IDisposable disp = obj as IDisposable;
				if( disp != null )
					disp.Dispose();
			}
			finally
			{
				obj = default( T );
			}
		}

		public static float Clamp( float val, float min, float max )
		{
			if( val < min ) return min;
			if( val > max ) return max;
			return val;
		}

		public static int Clamp( int val, int min, int max )
		{
			if( val < min ) return min;
			if( val > max ) return max;
			return val;
		}

		public static float Saturate( float val )
		{
			return Clamp( val, 0, 1 );
		}
	}

}